function fcombineMOHU(doc1,doc2,stylepairs,mohusign)
%将doc1选定的内容复制到doc2里
%stylepairs为可选项，用于两文档样式不一，需对照的情况，第一列为doc1样式，第二列为doc2样式。正文对应样式放在第一行
%在识别doc1的样式时会自动去掉空格，因此stylepairs第一列样式名称输入时不要去掉空格
%mohusign为可选项，默认为false;若为true,则会将没有匹配到的样式根据其格式居中缩进等进行调整(调整规则需在代码中各mohusign=true对应的逻辑分支手动设置),适用文档样式混乱的情况
try
    % 若Word服务器已经打开，返回其句柄Word
    Word = actxGetRunningServer('Word.Application');
catch
    % 创建一个Microsoft Word服务器，返回句柄Word
    Word = actxserver('Word.Application');
end
old_PasteFormatBetweenStyledDocuments=Word.Options.PasteFormatBetweenStyledDocuments;%样式冲突时需要以新文档样式为准
Word.Options.PasteFormatBetweenStyledDocuments='wdUseDestinationStyles';
old_PasteFormatBetweenDocuments=Word.Options.PasteFormatBetweenDocuments;
Word.Options.PasteFormatBetweenDocuments='wdKeepSourceFormatting';
%不输入stylepairs的情况
if exist('stylepairs','var')==false
    stylepairs={'正文','正文'};
end

for i=1:size(stylepairs,1)
    for j=1:2
        rsss=stylepairs(i,j);
        rsss(isspace(rsss)) = [];
        stylepairs(i,j)=rsss;
    end
end

mstylepairs=stylepairs(2:end,:);%样式名中含有该字段的,即认为匹配上了
if exist('mohusign','var')==false
    mohusign=false;
end
%%
CCC=Word.Documents.Item(doc1);
CCC.Activate;
Selection=Word.Selection;
AAA=Selection.Range;
pnum=AAA.Paragraphs.Count;

BBB=Word.Documents.Item(doc2);
BBB.Activate;%表明之后的粘贴都在BBB上
i=1;
while (i<=pnum)
    for tesi=1:10
        if tesi>9
            AAA.Paragraphs.Item(i).Range.HighlightColorIndex='wdRed';
            error('请重试office')
        end
        try
            AparaR= AAA.Paragraphs.Item(i).Range;
            break
        end
    end
    if AparaR.Information('wdWithinTable')
        for tesi=1:10
            if tesi>9
                AparaR.Tables.Item(1).Range.HighlightColorIndex='wdRed';
                error('请重试office')
            end
            try
                rrr=AparaR.Tables.Item(1).Range;
                Tnum=rrr.Paragraphs.Count;
                rrr.Copy;
                BBB.Fields.Locked=-1;%粘贴前锁定全文域
                Selection = Word.Selection;
                oo1=Selection.Start;
                Selection.Paste;
                break
            end
        end
        Selection = Word.Selection;
        oo2=Selection.End;
        Selection.Fields.Update;
        BRange=BBB.Range(oo1,oo2);
        Bgraphs=BRange.Paragraphs;
        npara=Bgraphs.Count;
        oldStyle=strings(npara,1);
        notab=ones(1,npara);
        %读取旧的样式
        for ipara=1:npara
            oldStyle(ipara)='';
            rpara=rrr.Paragraphs.Item(ipara);
            text=rpara.Range.Text;
            if strcmp(text,char([13,7]))
                try
                    rpara.Range.Text='';
                catch
                    notab(ipara)=0;
                    continue;%根据表格的换行符无法被更改文字来识别,防止它的样式干扰
                end
            end
            try
                oldStyle(ipara)=rpara.Style.NameLocal;
            catch
                oldStyle(ipara)=stylepairs{1,1};
            end
        end
        notabindex=find(notab);%只有不属于表格换行符的才进行后续操作
        %根据规则计算新的样式
        newStyle=strings(npara,1);
        [uoldstyles, ~, ic] = unique(oldStyle);
        for ipara=1:numel(uoldstyles)
            nstl='';
            parastyle=uoldstyles(ipara);
            parastyle(isspace(parastyle)) = [];
            rss=find(strcmp(parastyle,stylepairs(:,1))~=0,1);
            if rss
                nstl=stylepairs{rss,2};
            else
                trss=find(cellfun(@(style)contains(parastyle, style), mstylepairs(:,1))~=0,1,'last');
                if trss
                    nstl=mstylepairs{trss,2};
                end
            end
            newStyle(ic==ipara)=nstl;
        end
        %
        if mohusign%根据实际段落格式匹配
            for ipara=notabindex
                parastyle=oldStyle(ipara);
                parastyle(isspace(parastyle)) = [];
                % if (any(strcmp(AparaR.Tables.Item(1).Cell(1,1).Shading.BackgroundPatternColorIndex,{'wdNoHighlight','wdAuto'}))==false)%对于有高亮的表格要区别对待
                %     % if strcmp(parastyle,'正文')
                %     newStyle{ipara}='带颜色表格';
                %     % end
                % end
                % if (rrr.InlineShapes.count==0)&isempty(char(newStyle(ipara)))%表格内无图片,且没找到匹配的样式
                %     newStyle(ipara)='表格-文字';
                % end
                if (rrr.InlineShapes.count==0)%表格内无图片,则应该用表格-文字
                    newStyle(ipara)='表格-文字';
                end
            end
        end
        uns=unique(newStyle(notabindex));%除表格回车符之外,需要进行操作的样式
        if numel(uns)>1
            for ipara=notabindex
                if isempty(char(newStyle(ipara)))==false
                    Bgraphs.Item(ipara).Range.Style=newStyle(ipara);
                end
            end
        elseif not(isempty(char(uns(1))))%如果要操作的样式都一样,直接整个表格变了
            BRange.Style=uns(1);
        end
        if 1%mohusign%根据实际段落格式匹配
            Btable=BRange.Tables.Item(1);
            pageRight = BRange.PageSetup.PageWidth - BRange.PageSetup.RightMargin;
            Width=Btable.Range.Information('wdHorizontalPositionRelativeToPage');
            ii=1;
            while(1)
                try
                    Width=Width+Btable.Cell(1, ii).Width;
                catch
                    break;
                end
                ii=ii+1;
            end
            if Width > pageRight%判断表格是否超范围
                Btable.AutoFitBehavior('wdAutoFitWindow');%根据页面宽度调整表格
            end
        end
        i=i+Tnum;
    else
        for tesi=1:10
            if tesi>9
                AparaR.HighlightColorIndex='wdRed';
                error('请重试office')
            end
            try
                AparaR.Copy;
                Selection = Word.Selection;
                Selection.Paste;
                break
            end
        end

        rrr=BBB.Range(0,Selection.Paragraphs.Item(1).Range.End).Paragraphs.Count-1;
        Bpara=BBB.Paragraphs.Item(rrr);
        Bpara.Range.Fields.Update;
        %%
        try
            parastyle=AparaR.Style.NameLocal;
        catch
            parastyle=stylepairs{1,1};
        end
        parastyle(isspace(parastyle)) = [];

        rss=find(strcmp(parastyle,stylepairs(:,1))~=0,1);
        if rss
            Bpara.Range.Style=stylepairs{rss,2};
        else
            trss=find(cellfun(@(style)contains(parastyle, style), mstylepairs(:,1))~=0,1,"last");
            if trss
                Bpara.Range.Style=mstylepairs{trss,2};
            elseif mohusign%根据实际段落格式匹配
                if (strcmp(AAA.Paragraphs.Item(i).Alignment,'wdAlignParagraphCenter')==false)&(AAA.Paragraphs.Item(i).CharacterUnitFirstLineIndent==2)%首行缩进2格
                    Bpara.Range.Style=stylepairs{1,2};
                end
            end
        end
        if mohusign%根据实际段落格式匹配
            if strcmp(AAA.Paragraphs.Item(i).Alignment,'wdAlignParagraphCenter')&(Bpara.Range.InlineShapes.count>0)%公式图片等居中
                Bpara.Alignment='wdAlignParagraphCenter';
            end
        end
        %%
        i=i+1;
    end
end
% BBB.Fields.Locked=0;%解锁域
% BBB.Fields.Update
% BBB.save;
Word.Options.PasteFormatBetweenStyledDocuments=old_PasteFormatBetweenStyledDocuments;
Word.Options.PasteFormatBetweenDocuments=old_PasteFormatBetweenDocuments;
end